1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.collect;
18
19 import static com.google.common.collect.BoundType.CLOSED;
20 import static com.google.common.collect.BoundType.OPEN;
21
22 import com.google.common.annotations.GwtCompatible;
23 import com.google.common.collect.Multiset.Entry;
24
25 import java.util.Comparator;
26 import java.util.NoSuchElementException;
27 import java.util.SortedSet;
28
29 import javax.annotation.Nullable;
30
31
32
33
34
35
36
37 @GwtCompatible(emulated = true)
38 final class SortedMultisets {
39 private SortedMultisets() {
40 }
41
42
43
44
45 static class ElementSet<E> extends Multisets.ElementSet<E> implements
46 SortedSet<E> {
47 private final SortedMultiset<E> multiset;
48
49 ElementSet(SortedMultiset<E> multiset) {
50 this.multiset = multiset;
51 }
52
53 @Override final SortedMultiset<E> multiset() {
54 return multiset;
55 }
56
57 @Override public Comparator<? super E> comparator() {
58 return multiset().comparator();
59 }
60
61 @Override public SortedSet<E> subSet(E fromElement, E toElement) {
62 return multiset().subMultiset(fromElement, CLOSED, toElement, OPEN).elementSet();
63 }
64
65 @Override public SortedSet<E> headSet(E toElement) {
66 return multiset().headMultiset(toElement, OPEN).elementSet();
67 }
68
69 @Override public SortedSet<E> tailSet(E fromElement) {
70 return multiset().tailMultiset(fromElement, CLOSED).elementSet();
71 }
72
73 @Override public E first() {
74 return getElementOrThrow(multiset().firstEntry());
75 }
76
77 @Override public E last() {
78 return getElementOrThrow(multiset().lastEntry());
79 }
80 }
81
82 private static <E> E getElementOrThrow(Entry<E> entry) {
83 if (entry == null) {
84 throw new NoSuchElementException();
85 }
86 return entry.getElement();
87 }
88
89 private static <E> E getElementOrNull(@Nullable Entry<E> entry) {
90 return (entry == null) ? null : entry.getElement();
91 }
92 }
93